
const customRequst = require('../../../common/customRequst');
let app = getApp();
Page({
  data: {
    orderId: '',
    fileList: [],
    filePath:'',
    Formtoken: '',
    isSubmit: false,
  },
  onLoad: function (options) {
    let that = this
    if (options.id) {
      that.setData({
        orderId: options.id
      })
    }
    customRequst.postRequest('api/common/get_form_token', {}).then(res => {
      if (res.code == 1) {
        that.setData({
          formToken: res.data.token
        })
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    })
  },

  onShow: function () {
    let that = this
  },

  delete(event) {
    this.setData({
      fileList:[],
      filePath:'',
    })
  },
  // 上传
  afterRead(event) {
    let that = this
    const {
      file
    } = event.detail;
    wx.uploadFile({
      url: app.globalData.workAipDomain + 'api/common/upload_file',
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        let json = JSON.parse(res.data);
        that.setData({
          fileList:[{
            ...file,
            url: json.data.fullurl,
            isImage:true
          }],
          filePath:json.data.url,
        });
      },
    });
  },
  formSubmit:function(e){
    let that = this
    if (that.data.isSubmit) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'none'
      })
      return;
    }
  
    if (!that.data.filePath) {
      wx.showToast({
        title: '投稿文件不能为空',
        icon: 'none'
      })
      return;
    }
    let remark = e.detail.value.remark
  
    that.setData({
      isSubmit: true
    })
    customRequst.postRequest('api/contribution/setpayimg', {
      id: that.data.orderId,
      token: that.data.formToken,
      pay_image: that.data.filePath,
      pay_remark:remark,
    }).then(res => {

      that.setData({
        isSubmit: false
      });
      if (res.code == 1) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          mask:true
        });
        setTimeout(function() {
          wx.navigateTo({
            url: '/pages/order/list/index?type=2',
          })
       }, 2000);
      }else {
        wx.showToast({
          title: res.msg,
          mask:true,
          icon:none,
          success: function () {
            that.setData({
              Formtoken:res.data.token
            });
          }
        });
      }
    })
  },

})